package com.hrb.douban.mview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hrb.douban.R;

public class CustomTitleBar extends RelativeLayout {
    private TextView mtitle;
    private Button mlBtn;
    private Button mrBtn;
    public CustomTitleBar(Context context) {
        super(context);
    }


    public CustomTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.custom_title_bar_layout,this,true);
        mtitle=findViewById(R.id.title_bar_title);
        mlBtn=findViewById(R.id.title_bar_left);
        mrBtn=findViewById(R.id.title_bar_right);
        TypedArray attributes=context.obtainStyledAttributes(attrs,R.styleable.CustomTitleBar);
        if(attributes!=null){
            //设置背景颜色
            int titleBarGround=attributes.getResourceId(R.styleable.CustomTitleBar_title_background_color,Color.RED);
            setBackgroundResource(titleBarGround);
            //setBackgroundResource(attributes.getResourceId(R.styleable.CustomTitleBar_title_background_color,Color.RED));
            //控制是否要显示左侧按钮
            boolean leftBtnVisible=attributes.getBoolean(R.styleable.CustomTitleBar_left_button_visible,true);
            if (leftBtnVisible){
                mlBtn.setVisibility(View.VISIBLE);
            }else{
                mlBtn.setVisibility(View.INVISIBLE);
            }
            //设置左边按钮的文字
            String left_text=attributes.getString(R.styleable.CustomTitleBar_left_button_text);
            if(!TextUtils.isEmpty(left_text)){
                mlBtn.setText(left_text);
                //设置颜色
                mlBtn.setTextColor(attributes.getColor(R.styleable.CustomTitleBar_left_button_text_color,Color.WHITE));
            }else {
                //设置左边图片icon 这里设置二选一 文字或是图片
                int leftIcon=attributes.getResourceId(R.styleable.CustomTitleBar_left_button_drawable,R.drawable.back);
                if(leftIcon!=-1){
//              mlBtn.setCompoundDrawablesRelativeWithIntrinsicBounds(leftIcon,0,0,0);//设置到控件的位置
                    Drawable drawable=getResources().getDrawable(leftIcon);
                    drawable.setBounds(0,5,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                    mlBtn.setCompoundDrawables(drawable,null,null,null);
                }
            }
            //处理标题 先获取标题是否要显示图片icon
            int titleTextIcon=attributes.getResourceId(R.styleable.CustomTitleBar_title_text_drawable,-1);
            if(titleTextIcon!=-1){
                mtitle.setBackgroundResource(titleTextIcon);
            }else{
                //不是图片显示文字
                String titleText=attributes.getString(R.styleable.CustomTitleBar_title_text);
                if(!TextUtils.isEmpty(titleText)){
                    mtitle.setText(titleText);
                }
                //获取标题颜色
                int titltextColor= attributes.getColor(R.styleable.CustomTitleBar_title_text_color,Color.WHITE);
                mtitle.setTextColor(titltextColor);
            }
            //是否要显示右侧按钮
            boolean mrightBtnVisible= attributes.getBoolean(R.styleable.CustomTitleBar_right_button_visible,true);
            if(mrightBtnVisible){
                mrBtn.setVisibility(View.VISIBLE);
            }else{
                mrBtn.setVisibility(View.INVISIBLE);
            }

            // 设置右边图片icon 这里设置二选一 文字或是图片 同时设置时显示图片
            int rightButtonIcon =attributes.getResourceId(R.styleable.CustomTitleBar_right_button_drawable,-1);
            if(rightButtonIcon!=-1){
                Drawable drawable=getResources().getDrawable(rightButtonIcon);
                drawable.setBounds(0,5,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                mrBtn.setCompoundDrawables(drawable,null,null,null);
                //mrBtn.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,rightButtonIcon,0);
            }else{
                //按钮文字
                String rightText= attributes.getString(R.styleable.CustomTitleBar_right_button_text);
                if(!TextUtils.isEmpty(rightText)){
                    mrBtn.setText(rightText);
                    //设置文字颜色
                    int rcolor =attributes.getColor(R.styleable.CustomTitleBar_right_button_text_color,Color.WHITE);
                    mrBtn.setTextColor(rcolor);
                }
            }
            attributes.recycle();

        }

    }

    public void setTitleClickListener(OnClickListener listener){
        if(listener!=null){
            mlBtn.setOnClickListener(listener);
            mrBtn.setOnClickListener(listener);
        }
    }
    public CustomTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TextView getMtitle() {
        return mtitle;
    }

    public Button getMlBtn() {
        return mlBtn;
    }

    public Button getMrBtn() {
        return mrBtn;
    }
}
